# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\mailer\settings_loader.py
import logging
from sqlalchemy import select as sql_select
from db import Connection
from db.tables.system_config import SystemConfigRow

_logger = logging.getLogger("helpers.mailer.settings_loader")


def on_premise_settings_loader(shard):
    result = None
    with Connection(shard) as (connection):
        q = sql_select((SystemConfigRow.value,)).where(
            SystemConfigRow.name == "notifications"
        )
        s = connection.execute(q).fetchone()
        if (
            s
            and s.value
            and s.value.get("address")
            and s.value.get("disabled") is not False
        ):
            result = dict(
                address=s.value["address"],
                port=s.value.get("port"),
                username=s.value.get("username"),
                password=s.value.get("password"),
                from_address=s.value.get("from_address"),
                security=s.value.get("security"),
            )
            if result["port"] is None:
                if result["security"] == "ssl":
                    result["port"] = 465
                else:
                    if result["security"] == "tls":
                        result["port"] = 587
                    else:
                        result["port"] = 25
        if result["security"] == "auto":
            if result["port"] == 465:
                result["security"] = "ssl"
            else:
                if result["port"] == 587:
                    result["security"] = "tls"
                else:
                    result["security"] = None
    return result
